package com.jh.user.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.jh.user.pojo.enums.ChannelMerType;
import com.jh.user.pojo.enums.PayType;
import com.jh.user.service.UserChannelRequest;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;


/**
 * 这个费率是用户在平台消费的费率
 * */
@Entity
@Table(name="t_channel_rate")
public class ChannelRate implements Serializable{

	private static final long serialVersionUID = 1L;

	public static ChannelRate initRate(User user, Short payType, Long channelId,String channelMerType) {
		ChannelRate channelRate = new ChannelRate();
		channelRate.setUserId(user.getId());
		channelRate.setBrandId(user.getBrandId());
		channelRate.setChannelId(channelId);
		channelRate.setPayType(payType);
		Date now = new Date();
		channelRate.setUpdateTime(now);
		channelRate.setCreateTime(now);
		channelRate.setChannelMerType(channelMerType);
		return channelRate;
	}

	public void supply(BigDecimal rate, BigDecimal extraFee, BigDecimal gradeRate, String brandRate) {
		setRate(rate == null ? BigDecimal.ZERO : rate);
		setExtraFee(extraFee == null ? BigDecimal.ZERO : extraFee);
		setGradeRate(gradeRate == null ? BigDecimal.ZERO : gradeRate);
		setBrandRate(brandRate == null ? BigDecimal.ZERO : new BigDecimal(brandRate));
		setUpdateTime(new Date());
	}

	public static ChannelRate initRate(User user, BrandRate brandRate) {
		ChannelRate channelRate = new ChannelRate();
		channelRate.setUserId(user.getId());
		channelRate.setBrandId(user.getBrandId());
		channelRate.setChannelId(brandRate.getChannelId());
		channelRate.setRate(brandRate.getRate() == null ? BigDecimal.ZERO : brandRate.getRate());
		channelRate.setExtraFee(brandRate.getExtraFee() == null ? BigDecimal.ZERO : brandRate.getExtraFee());
		channelRate.setGradeRate(brandRate.getGradeRate() == null ? BigDecimal.ZERO : brandRate.getGradeRate());
		channelRate.setPayType(brandRate.getPayType());
		Date now = new Date();
		channelRate.setUpdateTime(now);
		channelRate.setCreateTime(now);
		channelRate.setChannelMerType(brandRate.getChannelMerType());
		return channelRate;
	}

	public void update(BrandRate brandRate) {
		setRate(brandRate.getRate() == null ? BigDecimal.ZERO : brandRate.getRate());
		setExtraFee(brandRate.getExtraFee() == null ? BigDecimal.ZERO : brandRate.getExtraFee());
		setGradeRate(brandRate.getGradeRate() == null ? BigDecimal.ZERO : brandRate.getGradeRate());
		setUpdateTime(new Date());
	}

	/**
	 * 判断requestValue不为空，并判断三个值不一样，满足判断条件则需要修改
	 * (特定业务调用，慎用)
	 */
	public static boolean checkIfUpdate(BigDecimal channelValue, BigDecimal brandValue, BigDecimal requestValue) {
		return requestValue != null && brandValue.compareTo(channelValue) == 0 && requestValue.compareTo(brandValue) != 0;
	}

	public void setChannelRateFromBrandRate(BrandRate br) {
		setBrandId(br.getBrandId());
		setChannelId(br.getChannelId());
		setRate(br.getRate());
		setExtraFee(br.getExtraFee());
		setGradeRate(br.getGradeRate());
		setStatus(br.getStatus());
		setPayType(br.getPayType());
		setBrandRate(br.getBrandRate());
		setUpdateTime(Calendar.getInstance().getTime());
		setCreateTime(Calendar.getInstance().getTime());
		setChannelMerType(br.getChannelMerType());
	}

	public static ChannelRate getChannelRateFromUserChannelRequest(UserChannelRequest ucRequest) {
		ChannelRate channelRate = new ChannelRate();
		Long userId = ucRequest.getUserId();
		BigDecimal rate = ucRequest.getRate();
		BigDecimal extraFee = ucRequest.getExtraFee();
		Long channelId = ucRequest.getChannelId();
		Short type = ucRequest.getType();
		BigDecimal gradeRate = ucRequest.getGradeRate();
		String merType = ucRequest.getMerType();

		if (null != userId) {
			channelRate.setUserId(userId);
		}
		if (null != rate) {
			channelRate.setRate(rate);
		}
		if (null != extraFee) {
			channelRate.setExtraFee(extraFee);
		}
		if (null != channelId) {
			channelRate.setChannelId(channelId);
		}
		if (null != type) {
			channelRate.setPayType(type);
		}
		if (null != gradeRate) {
			channelRate.setGradeRate(gradeRate);
		}
		if (null != merType) {
			channelRate.setChannelMerType(merType);
		}
		return channelRate;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name="id")
	private long id; 
	
	/**用户id*/
	@Column(name="user_id")
	private long userId;
	
	/**所属贴牌， 如果userId为空的时候， 那么就是该平台的默认费率*/
	@Column(name="brand_id")
	private long brandId;
	
	/**通道的Id*/
	@Column(name="channel_id")
	private long channelId;

	/**通道的交易费率*/
	@Column(name="rate")
	private BigDecimal rate;
	
	/**单笔手续费*/
	@Column(name="extra_fee")
	private BigDecimal extraFee;

	/** 推荐人分润费率 */
	@Column(name = "grade_rate")
	private BigDecimal gradeRate;

	@Column(name = "pay_type")
	private short payType;

	@Column(name = "status")
	private String status = "1";

	/**
	 * 贴牌上费率
	 */
	@Column(name = "brand_rate")
	private BigDecimal brandRate;

	@Column(name = "update_time")
	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
	private Date updateTime;

	/**创建时间*/
	@Column(name="create_time")
	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
	private Date createTime;

	/**通道商户类型 1落地商户，2线上商户*/
	@Column(name = "channel_mer_type")
	private String channelMerType;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getUserId() {
		return userId;
	}

	public void setUserId(long userId) {
		this.userId = userId;
	}

	public long getChannelId() {
		return channelId;
	}

	public void setChannelId(long channelId) {
		this.channelId = channelId;
	}

	public BigDecimal getRate() {
		return rate;
	}

	public void setRate(BigDecimal rate) {
		this.rate = rate;
	}

	public BigDecimal getExtraFee() {
		return extraFee;
	}

	public void setExtraFee(BigDecimal extraFee) {
		this.extraFee = extraFee;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public long getBrandId() {
		return brandId;
	}

	public void setBrandId(long brandId) {
		this.brandId = brandId;
	}

	public short getPayType() {
		return payType;
	}

	public void setPayType(short payType) {
		this.payType = payType;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public BigDecimal getGradeRate() {
		return gradeRate;
	}

	public void setGradeRate(BigDecimal gradeRate) {
		this.gradeRate = gradeRate;
	}

	public PayType ofPayType() {
		return PayType.of(getPayType());
	}

	public BigDecimal getBrandRate() {
		return brandRate;
	}

	public void setBrandRate(BigDecimal brandRate) {
		this.brandRate = brandRate;
	}

	public String getChannelMerType() {
		return channelMerType;
	}

	public void setChannelMerType(String channelMerType) {
		this.channelMerType = channelMerType;
	}

	public ChannelMerType ofMerType() {
		return ChannelMerType.of(getChannelMerType());
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		ChannelRate that = (ChannelRate) o;
		return id == that.id &&
				userId == that.userId &&
				brandId == that.brandId &&
				channelId == that.channelId &&
				Objects.equals(rate, that.rate) &&
				Objects.equals(extraFee, that.extraFee) &&
				Objects.equals(gradeRate, that.gradeRate) &&
				Objects.equals(payType, that.payType) &&
				Objects.equals(status, that.status) &&
				Objects.equals(brandRate, that.brandRate) &&
				Objects.equals(updateTime, that.updateTime) &&
				Objects.equals(createTime, that.createTime) &&
				Objects.equals(channelMerType, that.channelMerType);
	}

	@Override
	public int hashCode() {
		return Objects.hash(id, userId, brandId, channelId, rate, extraFee, gradeRate, payType, status, brandRate, updateTime, createTime, channelMerType);
	}

	@Override
	public String toString() {
		return "ChannelRate{" +
				"id=" + id +
				", userId=" + userId +
				", brandId=" + brandId +
				", channelId=" + channelId +
				", rate=" + rate +
				", extraFee=" + extraFee +
				", gradeRate=" + gradeRate +
				", payType=" + payType +
				", status='" + status + '\'' +
				", brandRate=" + brandRate +
				", updateTime=" + updateTime +
				", createTime=" + createTime +
				", channelMerType=" + channelMerType +
				'}';
	}

    public void addChannelRate(BrandRate brandRate, long userId, long brandId) {
		setUserId(userId);
		setBrandId(brandId);
		setChannelId(brandRate.getChannelId());
		setRate(brandRate.getRate());
		setExtraFee(brandRate.getExtraFee());
		setGradeRate(brandRate.getGradeRate());
		setStatus(brandRate.getStatus());
		setPayType(brandRate.getPayType());
		setUpdateTime(Calendar.getInstance().getTime());
		setCreateTime(Calendar.getInstance().getTime());
		setBrandRate(brandRate.getBrandRate());
		setChannelMerType(brandRate.getChannelMerType());
    }
}
